package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.config.CodeConfig;
import com.campus.entity.Clazz.Clazz;
import com.campus.entity.ResultBody;
import com.campus.entity.System.SysUser;
import com.campus.mapper.ClazzMapper;
import com.campus.mapper.StudentMapper;
import com.campus.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;

/**
 * @author Alaskaboo
 * @description 针对表【t_clazz(班级表)】的数据库操作Service实现
 * @createDate 2022-10-24 11:10:14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;


    public ResultBody saveClazz(@RequestBody Clazz clazz) {
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        clazz.setCreateTime(new Date());
        SysUser user = new SysUser();
        clazz.setCreateBy(user.getAccount());
        try {
            queryWrapper.eq("school", clazz.getSchool()).eq("class_num", clazz.getClassNum());
            if (clazzMapper.selectCount(queryWrapper) > 0) {
                return ResultBody.error().message("注册失败，该班级已经被注册，请重试！").describe("注册失败，该班级已经被注册，请重试！");
            }
            clazzMapper.insert(clazz);
        } catch (Exception e) {
            return ResultBody.error().message("注册失败，该班级已经被注册，请重试！").describe("注册失败，该班级已经被注册，请重试！");
        }
        return ResultBody.ok().message(CodeConfig.SAVE_SUCCESS.message).data("clazz", clazz);
    }

    @Override
    public IPage<Clazz> getClazzPage(Page<Clazz> page, Clazz clazz) {
        //判断要模糊查询的数据是否为空，在放进条件控制器queryWrapper
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(clazz.getSchool())) {
            queryWrapper.like("school", clazz.getSchool());
        }
        if(!StringUtils.isEmpty(clazz.getCollege())){
            queryWrapper.like("college", clazz.getCollege());
        }
        if(!StringUtils.isEmpty(clazz.getMajor())){
            queryWrapper.like("major", clazz.getMajor());
        }
        if(!StringUtils.isEmpty(clazz.getClasses())){
            queryWrapper.like("classes", clazz.getClasses());
        }
        if (clazz.getClassNum()!=null) {
            queryWrapper.like("class_num", clazz.getClassNum());
        }
        queryWrapper.orderByDesc("id");
        Page<Clazz> clazzPage = baseMapper.selectPage(page, queryWrapper);
        return clazzPage;
    }

}




